<?php

namespace App\Http\Controllers\Client;

use App\Model\Category;
use App\Model\Meta;
use App\Model\Page;
use App\Services\CommentService;
use App\Services\Util;
use Illuminate\Support\Facades\Input;

class IndexController extends ClientController
{
    public function index()
    {
        $orm = new Page();
        $p = Input::get('p',5);
        $orm = $orm->where('type',Page::TYPE_POST)->orderBy('created_at','desc');
        $navs = array();
        $date = Input::get('date');
        if(!empty($date)){
            array_push($navs,Util::array2Obj(array('key'=>'date','val'=>$date)));
            $st = Util::getMonth($date)[0];
            $et = Util::getMonth($date)[1];
            $orm = $orm->where('created_at','>',$st)->where('created_at','<',$et);
        }
        $keyword = Input::get('keyword');
        if(!empty($keyword)){
            $orm = $orm->where('title','like','%'.$keyword.'%');
        }
        $tag = Input::get('tag');
        if(!empty($tag)){
            $tag = Category::where('name',$tag)->where('type',Category::TYPE_TAG)->first();
            if(empty($tag)){
                return self::error('标签错误');
            }
            $ids  = array();
            $_tag = '%"' . $tag->id . '"%';
            $metas = Meta::where('tags','like',$_tag)->get(['page_id']);
            foreach ($metas as $meta){
                array_push($ids,$meta->page_id);
            }
            $orm = $orm->whereIn('id',$ids);
            array_push($navs,Util::array2Obj(array('key'=>'tag','val'=>$tag->name)));
        }
        $cate = Input::get('cate');
        if(!empty($cate)){
            $cate = Category::where('name',$cate)->where('type',Category::TYPE_CATE)->first();
            if(empty($cate)){
                return self::error('标签错误');
            }
            $ids  = array();
            $_cate = '%"' . $cate->id . '"%';
            $metas = Meta::where('tags','like',$_cate)->get(['page_id']);
            foreach ($metas as $meta){
                array_push($ids,$meta->page_id);
            }
            $orm = $orm->whereIn('id',$ids);
            array_push($navs,Util::array2Obj(array('key'=>'cate','val'=>$cate->name)));
        }
        $posts = $orm->paginate($p);
        $posts->setPath(url('').'?date='.$date.'&keyword='.$keyword.'&tag='.$tag);
        $pages = Page::where('type',Page::TYPE_PAGE)->get();
        $tags  = Category::where('type',Category::TYPE_TAG)->get();


        return view('index.index',['pages'=>$pages,'posts'=>$posts,'tags'=>$tags,'navlist'=>$navs]);
    }

    public function view($id)
    {
        $post = Page::where('id',$id)->where('type',Page::TYPE_POST)->first();
        if(empty($id)||empty($post)){
            return self::error('空了');
        }
        $commentSrv = new CommentService();
        $post->comment = $commentSrv->getComments($post->id);
        $meta = Meta::where('page_id',$post->id)->first();
        $meta->views = (int)$meta->views + 1;
        $meta->save();
        $recents = Page::where('type',Page::TYPE_POST)->orderBy('created_at','desc')->limit(5)->get();
        return view('index.view',['post'=>$post,'recents'=>$recents]);
    }
}
